package uutalk.netcloud.talk.base.widget;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import uutalk.netcloud.talk.R;
import uutalk.netcloud.talk.base.util.view.ResourcesUtil;


public class CenterEditTips
{
    public static final int TYPE_SINGLE = 1;
    public static final int TYPE_DOUBLE = 2;
    
    private View anchorView;
    /**
     * popWindow里的核心view
     */
    private PopupWindow popupWindow;
    private int screenHeight, screenWidth;
    private Activity context;
    TextView title, separation;
    Button sureButton, cancelButton;
    OnClickListener clickListener;
    private int type = TYPE_SINGLE;
    
    // ADD
    private TextView editID;
    private EditText editText;
    
    public void setAnchorView(View view)
    {
        this.anchorView = view;
    }

    public void setType(int type)
    {
        this.type = type;
        if (type == TYPE_SINGLE)
        {
            cancelButton.setVisibility(View.GONE);
            Drawable btnDrawable = context.getResources().getDrawable(R.drawable.btn_corner_single_right);
            sureButton.setBackgroundDrawable(btnDrawable);
        }
        else
        {
            Drawable btnDrawable = context.getResources().getDrawable(R.drawable.btn_corner_right);
            sureButton.setBackgroundDrawable(btnDrawable);
            cancelButton.setVisibility(View.VISIBLE);
        }
    }

    public void setTitleStr(String titleStr)
    {
        title.setText(titleStr);
    }
    
    public void setTitleGravity(int gravity)
    {
        //title.setGravity(gravity);
        LinearLayout.LayoutParams params =  (LinearLayout.LayoutParams) title.getLayoutParams();
        params.gravity = gravity;
        title.setLayoutParams(params);
    }

    public void setClickListener(OnClickListener clickListener)
    {
        this.clickListener = clickListener;
    }

    public CenterEditTips(Activity context)
    {
        this.context = context;
        screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        screenHeight = context.getResources().getDisplayMetrics().heightPixels;
        initPopWindow();
    }

    /**
     * 构造popWindow
     */
    private void initPopWindow()
    {
        RelativeLayout popView = new RelativeLayout(context);
        
        //popView.setBackgroundColor(Color.parseColor("#aa000000"));
        popView.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33333333")));
        popView.setLayoutParams(new LayoutParams(-1, -1));

        LinearLayout contentLine = (LinearLayout) View.inflate(context, R.layout.center_tips_layout, null);
        //LayoutParams pp = new LayoutParams(-1, AndroidUtil.dip2px(context, 100));
        LayoutParams pp = new LayoutParams(-1, LayoutParams.WRAP_CONTENT);
        pp.setMargins(ResourcesUtil.dp2px(20), 0, ResourcesUtil.dp2px(20), 0);
        pp.addRule(RelativeLayout.CENTER_IN_PARENT);
        contentLine.setLayoutParams(pp);

        title = (TextView) contentLine.findViewById(R.id.title);
        separation = (TextView) contentLine.findViewById(R.id.separation);
        sureButton = (Button) contentLine.findViewById(R.id.sure);
        cancelButton = (Button) contentLine.findViewById(R.id.cencel);
        editID = (TextView) contentLine.findViewById(R.id.edit_channel_id);
        editText = (EditText) contentLine.findViewById(R.id.edit_channel_name);

        if (type == TYPE_SINGLE)
        {
            cancelButton.setVisibility(View.GONE);
            Drawable btnDrawable = context.getResources().getDrawable(R.drawable.btn_corner_single_right);
            sureButton.setBackgroundDrawable(btnDrawable);
        }
        else
        {
            cancelButton.setVisibility(View.VISIBLE);
        }

        sureButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                if (clickListener != null)
                {
                    clickListener.onSureClick();
                }
                hide();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v)
            {
                if (clickListener != null)
                {
                    clickListener.onCancelClick();
                }
                hide();
            }
        });
        popView.addView(contentLine);
        // 创建PopupWindow实例
        popupWindow = new PopupWindow(popView, screenWidth, screenHeight, true);
    }

    public void show()
    {
        if("".equals(title.getText())){
            title.setVisibility(View.GONE);
            separation.setVisibility(View.GONE);
        } else {
            title.setVisibility(View.VISIBLE);
            separation.setVisibility(View.VISIBLE);
        }
        if (popupWindow == null)
        {
            Log.d("ActionSheet", "can not show ActionSheet before setProp <buttons>");
            return;
        }
        try
        {
            popupWindow.showAtLocation(anchorView ==null ? context.getWindow().getDecorView() : anchorView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
        catch (Exception e)
        {
        }
    }

    public void hide()
    {
        try
        {
            if (popupWindow != null)
            {
                popupWindow.dismiss();
            }
        }
        catch (Exception e)
        {
        }
    }

    public interface OnClickListener
    {
        void onSureClick();

        void onCancelClick();
    }
    
    /**
     * @param buttonTitleStr 取消按钮的标题
     * 设置确定按钮的标题
     */
    public void setsureButtonStr(String buttonTitleStr)
    {
        sureButton.setText(buttonTitleStr);
    }
    /**
     * @param buttonTitleStr 取消按钮的标题
     * 设置取消按钮的标题
     */
    public void setCancelButtonStr(String buttonTitleStr)
    {
        cancelButton.setText(buttonTitleStr);
    }

    public String getEditTextString()
    {
    	return editText.getText().toString();
    }
    
    public void setEditIDVisible(int visiblity)
    {
    	editID.setVisibility(View.VISIBLE);
    }
    
    public String getEditIDStr()
    {
    	return editID.getText().toString();
    }

    public void setEditIDStr(String str)
    {
        editID.setText(str);
    }
}
